﻿using MediatR;
using Traceability.App.API.Application.Command.HealthInformation;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.HealthInformation
{
    public class DelHealthHandler : IRequestHandler<DelHealthCommand, APIResult<int>>
    {
        private readonly IBaseRepository<HealthInformationModel> healthRepo;

        public DelHealthHandler(IBaseRepository<HealthInformationModel> healthRepo)
        {
            this.healthRepo = healthRepo;
        }
        public Task<APIResult<int>> Handle(DelHealthCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = healthRepo.GetAll().FirstOrDefault(x => x.Id == request.Id && x.Batch == request.Batch);
            res.IsDel = true;
            int r = healthRepo.UpdateAsync(res).Result;
            if (r > 0)
            {
                result.Message = "删除保健基本信息成功";
                result.Data = r;
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "删除保健基本信息失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return Task.FromResult(result);
        }
    }
}
